package com.sjc.designpattern.proxy;

/**
 * @Classname ProxyPatternDemo
 * @Description 代理模式：本地接口代理远程接口的调用
 * 1. Hibernate,mybatis 懒加载
 * 2. RPC远程调用
 * @Date 2020/4/25 21:30
 * @Created by SangJiacun
 */
public class ProxyPatternDemo {
    public static void main(String[] args) {
        Subject subject = new ConcreteSubject();
        Proxy proxy = new Proxy(subject);
        proxy.request();
    }

    public interface Subject{
        void request();
    }

    public static class ConcreteSubject implements Subject{

        @Override
        public void request() {
            System.out.println("执行请求！");
        }
    }

    public static class Proxy implements Subject{

        private Subject subject;

        public Proxy(Subject subject) {
            this.subject = subject;
        }

        @Override
        public void request() {
            System.out.println("执行额外的条件判断，拷贝是否要调用subject的request()方法");
            boolean invoke = true;
            if(invoke) {
                subject.request();
            }
        }
    }
}
